using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslDesign = global::Microsoft.VisualStudio.Modeling.Design;
using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;
using System;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Guava.ChannelTransformations
{
    public partial class ChannelTransformationsDomainModel
    {

        protected override System.Type[] GetCustomDomainModelTypes()
        {
           return new System.Type[] { typeof(MyChangePropertyRule), typeof(MyDeleteElementRule) };
        }
    }

    [RuleOn(typeof(Operators), FireTime = TimeToFire.TopLevelCommit)]
    public sealed class MyDeleteElementRule : DeletingRule
    {
        public override void ElementDeleting(ElementDeletingEventArgs e)
        {//fix the flow when an element is deleted
            Operators delete_operator = e.ModelElement.Store.ElementDirectory.FindElement(e.ModelElement.Id) as Operators;

            if (delete_operator == null) return;
            
            Operators opSource = delete_operator.FlowFrom;

            Operators opTarget = null;
            
            if (delete_operator.FlowTo.Count > 0) 
                opTarget =delete_operator.FlowTo[0];

            if ((opTarget == null) || (opSource == null)) return;

            using (Transaction t = opSource.Store.TransactionManager.BeginTransaction())
            {
                opSource.FlowTo.Clear();
                opTarget.FlowFrom = null;

                opSource.FlowTo.Add(opTarget);
                opTarget.FlowFrom = opSource;
                t.Commit();
                return;
            }
        }
    }
    [RuleOn(typeof(Operators),  FireTime = TimeToFire.TopLevelCommit)]
    public sealed class MyChangePropertyRule : ChangeRule
    {
        //make red icon appear or disappear depending on if all the properties are configured.(two types for now-ListType, string
        public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
        {
            bool emptyval = false;

            if ((e.DomainProperty.Name != "ShowConfigIcon")&& (e.DomainProperty.PropertyType.Name != "Function"))
            {
                Guava.ChannelTransformations.ListType tempval = e.NewValue as Guava.ChannelTransformations.ListType;
                if (tempval != null)
                {
                    if (tempval.Count == 0) emptyval =  true;
                }
                Guava.ChannelTransformations.MyDictionary tempDic = e.NewValue as Guava.ChannelTransformations.MyDictionary;
                if (tempDic != null)
                {
                    if (tempDic.Count == 0) emptyval = true;
                }
                string temp = e.NewValue as string;
                if (temp != null)
                {
                    if (temp == "") emptyval = true;
                }
                
            }
            
             // all other properties also need to non empty
             if (emptyval == false)
             {
                 foreach (DomainPropertyInfo di in e.DomainClass.AllDomainProperties)
                 {
                     if (di.GetValue(e.ModelElement) == null) return;
                     if (di.GetValue(e.ModelElement).GetType().Name == "ListType")
                     {
                         Guava.ChannelTransformations.ListType temp = di.GetValue(e.ModelElement) as Guava.ChannelTransformations.ListType;
                         if (temp != null)
                         {
                             if (temp.Count == 0)
                             {
                                 emptyval = true;
                                 break;
                             }

                         }
                     }
                     if (di.GetValue(e.ModelElement).GetType().Name == "MyDictionary")
                     {
                         Guava.ChannelTransformations.MyDictionary temp = di.GetValue(e.ModelElement) as Guava.ChannelTransformations.MyDictionary;
                         if (temp != null)
                         {
                             if (temp.Count == 0)
                             {
                                 emptyval = true;
                                 break;
                             }

                         }
                     }
                     if (di.GetValue(e.ModelElement).GetType().Name == "String")
                     {
                         if (di.GetValue(e.ModelElement).ToString() == "")
                         {
                             emptyval = true;
                             break;
                         }
                     }
                 }
             }
             DomainPropertyInfo propinfo = e.DomainClass.FindDomainProperty("ShowConfigIcon", true);
             if (propinfo != null)
             {
                 if ((emptyval == false) )
                     e.DomainClass.FindDomainProperty("ShowConfigIcon", true).SetValue(e.ModelElement, (global::System.Int16)1);
                 else
                     e.DomainClass.FindDomainProperty("ShowConfigIcon", true).SetValue(e.ModelElement, (global::System.Int16)0);
             }
            return;
            
        }
        

    }


   
}
